$(function(){
    Ext.onReady(function() {
        var obj = {
            formPanel: null,
            mapObj: null,
            marker: null,
            lng: null,
            lat: null,
            timer: null,
            djs: 0,
            init: function () {
                var that = this;
                var hy = Ext.create("Ext.data.Store",{
                    fields: ['name', 'id'],
                    data:[
                        {name:'加工',id:'加工'},
                        {name:'商务',id:'商务'},
                        {name:'培训',id:'培训'},
                        {name:'美食',id:'美食'},
                        {name:'家居',id:'家居'},
                        {name:'卖场',id:'卖场'},
                        {name:'房产',id:'房产'},
                        {name:'汽车',id:'汽车'},
                        {name:'机构',id:'机构'},
                        {name:'医疗',id:'医疗'},
                        {name:'婚假',id:'婚假'},
                        {name:'娱乐',id:'娱乐'},
                        {name:'家政',id:'家政'}
                    ]
                });
                var xs = Ext.create("Ext.data.Store",{
                    fields: ['name', 'id'],
                    data:[
                        {name:'越城',id:'越城'},
                        {name:'诸暨',id:'诸暨'},
                        {name:'上虞',id:'上虞'},
                        {name:'嵊州',id:'嵊州'},
                        {name:'新昌',id:'新昌'},
                        {name:'柯桥',id:'柯桥'}
                    ]
                });
                that.formPanel = Ext.create('Ext.form.Panel', {
                    renderTo: document.body,
                    xtype: 'form',
                    title: '注册',
                    width: '100%',
                    layoutConfig:{columns:3},
                    bodyPadding: 10,
                    defaultType: 'textfield',
                    items: [{
                        fieldLabel: '行业',
                        name: 'industry',
                        xtype:'combobox',
                        listConfig:{
                            emptyText:'没有找到匹配的数值',
                            maxHeight:200
                        },
                        queryMode:'local',//[local|remot]
                        store:hy,
                        valueField:"id",
                        displayField :'name',
                        forceSelection:true,//不允许输入数据集合中没有的数值
                        typeAhead : true,//自动补全，默认为false
                        value:'',
                        allowBlank: false,
                        tooltip: '请选择您的行业',
                        blankText: '请选择您的行业',
                        prompt: '请选择您的行业'
                    }, {
                        id: 'region',
                        fieldLabel: '所在区域',
                        name: 'region',
                        xtype:'combobox',
                        listConfig:{
                            emptyText:'没有找到匹配的数值',
                            maxHeight:200
                        },
                        queryMode:'local',//[local|remot]
                        store:xs,
                        valueField:"id",
                        displayField :'name',
                        forceSelection:true,//不允许输入数据集合中没有的数值
                        typeAhead : true,//自动补全，默认为false
                        value:'',
                        allowBlank: false,
                        tooltip: '请选择您的所在区域',
                        blankText: '请选择您的所在区域',
                        prompt: '请选择您的所在区域',
                        listeners: {
                            blur: function () {
                                var city = that.formPanel.getForm().findField('region').getValue();
                                that.mapObj.centerAndZoom(city,14);
                                that.mapObj.setCurrentCity(city);
                            }
                        }
                    }, {
                        id: 'companyName',
                        fieldLabel: '公司名称',
                        width: 400,
                        name: 'companyName',
                        allowBlank: false,
                        tooltip: '请输入您的公司名称',
                        blankText: '请输入您的公司名称',
                        listeners: {
                            blur: function () {
                                var companyName =  that.formPanel.getForm().findField('companyName').getValue();
                                if(companyName){
                                    var label = new BMap.Label(companyName,{offset:new BMap.Size(20,-10)});
                                    that.marker.setLabel(label);
                                }
                            }
                        }
                    }, {
                        fieldLabel: '公司地址',
                        id: 'companyAddress',
                        name: 'companyAddress',
                        width: 700,
                        allowBlank: false,
                        tooltip: '请输入您的公司地址',
                        blankText: '请输入您的公司地址',
                        listeners: {
                            render: function(obj) {
                                var font=document.createElement("font");
                                font.setAttribute("color","red");
                                var tips=document.createTextNode('（请在下方标注具体地理位置）');
                                font.appendChild(tips);
                                obj.el.dom.appendChild(font);
                            },
                            change: function () {
                                var companyAddress =  that.formPanel.getForm().findField('companyAddress').getValue();
                                var city = that.formPanel.getForm().findField('region').getValue();
                                if(companyAddress){
                                    that.mapObj.clearOverlays();
                                    var local = new BMap.LocalSearch('绍兴', {
                                        renderOptions:{map: that.mapObj,autoViewport: true,panel: "r-result"},
                                        pageCapacity: 8
                                    });
                                    local.search('绍兴'+city+companyAddress);
                                }
                            }
                        }
                    },{
                        xtype: 'panel',
                        width: '85%',
                        height: 400,
                        margin: '0 0 10px 105px',
                        html: '<div class="map" id="siteMap" style="height: 100%;width: 100%;"></div>'
                    }, {
                        fieldLabel: '联系人姓名',
                        name: 'contacts',
                        allowBlank: false,
                        tooltip: '请输入您的联系人姓名',
                        blankText: '请输入您的联系人姓名',
                    }, {
                        fieldLabel: '联系人电话',
                        name: 'contactsTel',
                        allowBlank: false,
                        tooltip: '请输入您的联系人电话',
                        blankText: '请输入您的联系人电话',
                    }, {
                        id: 'account',
                        fieldLabel: '账号',
                        name: 'account',
                        allowBlank: false,
                        tooltip: '请输入您的账号',
                        blankText: '请输入您的账号',
                    }, {
                        inputType:'password',
                        fieldLabel: '密码',
                        name: 'password',
                        tooltip: '请输入密码',
                        allowBlank: false,
                        blankText: '请输入密码',
                        regex: /^[\s\S]{0,32}$/,
                        regexText: '密码长度不能超过32个字符'
                    }, {
                        inputType:'password',
                        fieldLabel: '确认密码',
                        name: 'passwordAgain',
                        tooltip: '请输入密码',
                        allowBlank: false,
                        blankText: '请输入密码',
                        regex: /^[\s\S]{0,32}$/,
                        regexText: '密码长度不能超过32个字符'
                    }, {
                        inputType:'yzm',
                        fieldLabel: '短信验证码',
                        name: 'yzm',
                        tooltip: '请输入验证码',
                        allowBlank: false,
                        blankText: '请输入验证码'
                    }, {
                        id: 'getYZMBar',
                        xtype: 'button',
                        text: "发送验证码",
                        style: {
                            position: 'absolute',
                            left: '300px',
                            'margin-top': '-5px',
                            transform: 'translate(0,-100%);'
                        },
                        listeners: {
                            render: function (obj) {
                                var font=document.createElement("font");
                                font.setAttribute("id","djs");
                                font.setAttribute("color","red");
                                font.setAttribute("style","position:absolute;left: 400px;margin-top: -25px;");
                                var tips=document.createTextNode('');
                                font.appendChild(tips);

                                obj.el.dom.parentNode.appendChild(font);
                            },
                        },
                        handler: function() {
                            if(that.djs!=0){
                                $.alert('验证码发送频繁，请在'+that.djs+'s后重试',2800,'center');
                                return
                            }
                            that.startDJS(this);
                            that.ajaxGetYZM();
                        }
                    }
                    ],
                    buttons: [
                        {
                            text: '立即注册',
                            handler: function() {
                                var form = this.up('form'); // get the form panel
                                if (form.isValid()&&that.lng&&that.lat) {
                                    that.formPanel.form.doAction('submit', {
                                        url :$.getAjaxHost('def') + 'zpinfo/register',
                                        method : 'POST',
                                        params : {
                                            lng: that.lng,
                                            lat: that.lat
                                        },
                                        waitTitle : '连接中',
                                        waitMsg : '传送数据...',
                                        success : function() {
                                            Ext.Msg.alert('提示',
                                                '注册成功!'
                                            );
                                            window.history.go(-1);
                                        },
                                        failure : function(form, action) {
                                            Ext.Msg.alert('提示', '非常抱歉!,注册失败');
                                            that.formPanel.getForm().reset()
                                        }
                                    });

                                }else { // display error alert if the data is invalid
                                    if(!that.lng||!that.lat){
                                        Ext.Msg.alert('错误提示', '您需要在地图上标注具体地理位置。')
                                    }else{
                                        Ext.Msg.alert('错误提示', '请完善注册信息！')
                                    }
                                }
                            }
                        }
                    ]
                });
                that.mapInit();

            },
            mapInit: function () {
                var that = this;
                var opt = {
                    level:13,
                }
                that.mapObj = new BMap.Map("siteMap");    // 创建Map实例
                that.mapObj.centerAndZoom("绍兴",12);
                that.mapObj.setCurrentCity("绍兴");
                that.mapObj.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                that.mapObj.addEventListener("click",function(e){
                    that.mapObj.clearOverlays();
                    that.lng = e.point.lng;
                    that.lat = e.point.lat;
                    var point = new BMap.Point(e.point.lng, e.point.lat);
                    that.mapObj.centerAndZoom(point);
                    that.marker = new BMap.Marker(point);// 创建标注
                    that.mapObj.addOverlay(that.marker);
                    var companyName =  that.formPanel.getForm().findField('companyName').getValue();
                    if(companyName){
                        var label = new BMap.Label(companyName,{offset:new BMap.Size(20,-10)});
                        that.marker.setLabel(label);
                    }
                });
            },
            // 获取验证码
            ajaxGetYZM: function() {
                var that = this;
                var tel =Ext.getCmp("account").getValue();
                if(!tel){
                    $.alert('你需要先填写账号',2800,'center')
                }
                $.getData({
                    url: $.getAjaxHost('def') + 'zpinfo/getYZM',
                    //url: $.getAjaxHost('def') + 'cxjy/zpinfo/getList',
                    type: 'get',
                    contentType: 'application/json',
                    data: {
                        tel:encodeURI(tel),
                    },
                    success: function (res) {
                        $.alert('验证码发送成功',2800,'center')
                    },
                    error: function (res) {
                        that.djs = 0;
                        $.alert('验证码发送失败',2800,'center')
                    }
                })
            },
            // 倒计时
            startDJS:function (obj) {
                var that = this;
                clearInterval(that.timer);
                that.djs = 30;
                $('#djs').html(that.djs+'s后重试');
                that.timer = setInterval(function () {
                    if(that.djs>0){
                        $('#djs').html(that.djs+'s后重试');
                        that.djs --;
                    }else{
                        $('#djs').html('');
                        clearInterval(that.timer);
                    }
                },1000);
            }
        };
        obj.init();
    });
});